define(['jquery', 'bootstrap', 'backend', 'table', 'form','layui'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/baiying/byaitaskphone/index' + location.search,
                    add_url: 'zhongzhuan/baiying/byaitaskphone/add',
                    edit_url: 'zhongzhuan/baiying/byaitaskphone/edit',
                    del_url: 'zhongzhuan/baiying/byaitaskphone/del',
                    multi_url: 'zhongzhuan/baiying/byaitaskphone/multi',
                    import_url: 'zhongzhuan/baiying/byaitaskphone/import',
                    table: 'ec_ai_task_phone',
                }
            });

            var table = $("#table");
            
            table.on('post-common-search.bs.table', function (event, table) {

                // 以下2行代码是关键，是加载完表格后，把销售搜索空实例化成selectpage格式
                var form = $("form",table.$commonsearch);
                $("input[name='callJobId']",form).addClass("selectpage").data("source","zhongzhuan/fenpei/bytoec/indexselectpage").data("primaryKey","callJobId").data("field","zidingyi").data("orderBy","id asc");
                
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
//                        {field: 'is_yixiang', title: __('Is_yixiang')},
                        {field: 'yixiang_txt', title: __('Yixiang_txt'), operate: 'LIKE',searchable:false},
                        {field: 'yixiang_value', title: __('Yixiang_value'),addClass:"selectpicker", operate:"IN",data:"multiple", searchList: Config.yixiang_list},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: '分配类型', searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.status},
                        {field: 'callInstanceId', title: __('Callinstanceid'),searchable:false, operate: 'LIKE'},
                        {field: 'callJobId', title: __('Calljobid'), operate: 'LIKE'},
                        {field: 'customerTelephone', title: __('Customertelephone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customerName', title: __('Customername'),searchable:false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'startTime_text', title: __('Starttime'),searchable:false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endTime_text', title: __('Endtime'),searchable:false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'duration', searchable:false,title: __('Duration'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'luyinOssUrl', searchable:false,title: __('Luyinossurl'), operate: 'LIKE', formatter: Table.api.formatter.url},
//                        {field: 'config', title: __('Config')},
                        {field: 'createtime', title: __('Createtime'),defaultValue:this.today(), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '分配',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog',
                                    url: 'zhongzhuan/baiying/byaitaskphone/assignkehu',
                                    extend: 'data-area=["40%","90%"]', //这是控制弹窗的弹出的尺寸
                                    callback: function (data) {
//                                        table.bootstrapTable('refresh');
//                                        Layer.alert(JSON.stringify(data));
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status > 0){
                                            return false;
                                        }
                                        return true;
                                        
                                    }
                                }
                            ]
                        
                        }
                    ]
                ]
            });

            $(document).on("click",'.btn-batchassign', function () {
                
                var data = $('#table').bootstrapTable('getSelections');
                var selectIds= [];
                for(var i in data){
                    selectIds.push(data[i].id);
                }
                var selectIdStr = selectIds.join(",");
                console.log(data);
                layer.open({
                    type: 2,
                    area: ['40%','90%'], // 宽高
                    shade:0,
                    content:"/zQFZdrEgGv.php/zhongzhuan/baiying/byaitaskphone/assignkehu/ids/" + selectIdStr
//                    content: html + '<p style="padding: 11px;font-size:16px;line-height:30px;white-space:pre-line;">'+content+'</p>'
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index1: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/baiying/byaitaskphone/index1' + location.search,
                    add_url: 'zhongzhuan/baiying/byaitaskphone/add',
                    edit_url: 'zhongzhuan/baiying/byaitaskphone/edit',
                    del_url: 'zhongzhuan/baiying/byaitaskphone/del',
                    multi_url: 'zhongzhuan/baiying/byaitaskphone/multi',
                    import_url: 'zhongzhuan/baiying/byaitaskphone/import',
                    table: 'ec_ai_task_phone',
                }
            });

            var table = $("#table");
            
            table.on('post-common-search.bs.table', function (event, table) {

                // 以下2行代码是关键，是加载完表格后，把销售搜索空实例化成selectpage格式
                var form = $("form",table.$commonsearch);
                $("input[name='callJobId']",form).addClass("selectpage").data("source","zhongzhuan/fenpei/bytoec/indexselectpage").data("primaryKey","callJobId").data("field","zidingyi").data("orderBy","id asc");
                
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
//                        {field: 'is_yixiang', title: __('Is_yixiang')},
                        {field: 'yixiang_txt', title: __('Yixiang_txt'), operate: 'LIKE',searchable:false},
                        {field: 'yixiang_value', title: __('Yixiang_value'),searchable:false, operate: 'LIKE',searchList: Config.yixiang_list},
                        {field: 'status', title: __('Status'),searchable:false, searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: '分配类型', searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.status},
                        {field: 'callInstanceId', title: __('Callinstanceid'),searchable:false, operate: 'LIKE'},
                        {field: 'callJobId', title: __('Calljobid'), operate: 'LIKE'},
                        {field: 'customerTelephone', title: __('Customertelephone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customerName', title: __('Customername'),searchable:false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'startTime_text', title: __('Starttime'),searchable:false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endTime_text', title: __('Endtime'),searchable:false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'duration', searchable:false,title: __('Duration'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'luyinOssUrl', searchable:false,title: __('Luyinossurl'), operate: 'LIKE', formatter: Table.api.formatter.url},
//                        {field: 'config', title: __('Config')},
                        {field: 'createtime', title: __('Createtime'), defaultValue:this.today(),operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '分配',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog',
                                    url: 'zhongzhuan/baiying/byaitaskphone/assignkehu',
                                    extend: 'data-area=["40%","90%"]', //这是控制弹窗的弹出的尺寸
                                    callback: function (data) {
//                                        table.bootstrapTable('refresh');
//                                        Layer.alert(JSON.stringify(data));
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status > 0){
                                            return false;
                                        }
                                        return true;
                                        
                                    }
                                }
                            ]
                        
                        }
                    ]
                ]
            });

            $(document).on("click",'.btn-batchassign', function () {
                
                var data = $('#table').bootstrapTable('getSelections');
                var selectIds= [];
                for(var i in data){
                    selectIds.push(data[i].id);
                }
                var selectIdStr = selectIds.join(",");
                console.log(data);
                layer.open({
                    type: 2,
                    area: ['40%','90%'], // 宽高
                    shade:0,
                    content:"/zQFZdrEgGv.php/zhongzhuan/baiying/byaitaskphone/assignkehu/ids/" + selectIdStr
//                    content: html + '<p style="padding: 11px;font-size:16px;line-height:30px;white-space:pre-line;">'+content+'</p>'
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        //定义方法返回今日开始结束日期
        today: function () {
            const dd = new Date();
            const y = dd.getFullYear();// 获取今日年份
            const m = String(dd.getMonth() + 1).padStart(2, '0'); // 获取今日月份
            const d = String(dd.getDate()).padStart(2, '0'); // 获取今日日期

            const startOfDay = `${y}-${m}-${d} 00:00:00`;
            const endOfDay = `${y}-${m}-${d} 23:59:59`;

            return startOfDay + ' - ' + endOfDay;
        },
        edit: function () {
            Controller.api.bindevent();
        },
        assignkehu: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
